//package com.paishen.peiniwan.module.login;
//
//import android.content.Intent;
//import android.content.pm.ApplicationInfo;
//import android.content.pm.PackageManager;
//import android.os.Build;
//import android.os.Bundle;
//import android.text.Editable;
//import android.text.InputType;
//import android.text.TextUtils;
//import android.text.TextWatcher;
//import android.view.MotionEvent;
//import android.view.View;
//import android.widget.TextView;
//
//import com.paishen.common.dialog.LoadingDialog;
//import com.paishen.common.util.BaseUtil;
//import com.paishen.common.util.LogUtil;
//import com.paishen.common.util.SpfsUtil;
//import com.paishen.peiniwan.ActivityCollector;
//import com.paishen.peiniwan.App;
//import com.paishen.peiniwan.BaseActivity;
//import com.paishen.peiniwan.BusEvent;
//import com.paishen.peiniwan.C;
//import com.paishen.peiniwan.R;
//import com.paishen.peiniwan.database.UserDao;
//import com.paishen.peiniwan.database.util.ModelConverter;
//import com.paishen.peiniwan.frame.PnwOkCallbackAdapter;
//import com.paishen.peiniwan.frame.http.PnwHttpManager;
//import com.paishen.peiniwan.frame.http.entity.ServiceInfo;
//import com.paishen.peiniwan.frame.http.entity.User;
//import com.paishen.peiniwan.frame.http.response.HttpRespData;
//import com.paishen.peiniwan.frame.http.response.HttpRespUser;
//import com.paishen.peiniwan.frame.widget.InputEditLayout;
//import com.paishen.peiniwan.module.MainActivity;
//import com.squareup.okhttp.Request;
//import com.umeng.analytics.MobclickAgent;
//
//
//public class LoginActivity extends BaseActivity {
//
//    private InputEditLayout layEditPhone, layEditPassword;
//    private TextView btnLogin;
//    private View viewLine1, viewLine2;
//
//    private boolean checkLoginInfo(boolean remind) {
//        boolean result = layEditPhone.getText().length() == 11 && layEditPassword.getText().length() >= 6;
//        if (!result && remind) {
//            BaseUtil.showToast(R.string.login_fail);
//        }
//        return result;
//    }
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        isSupportTouchBack = false;
//        setContentView(R.layout.activity_login);
//
//        BaseUtil.setStatusBarDarkMode(true, this);
//
//        layEditPhone = (InputEditLayout) findViewById(R.id.lay_edit_phone);
//        layEditPhone.setInputType(InputType.TYPE_CLASS_NUMBER);
//        layEditPassword = (InputEditLayout) findViewById(R.id.lay_edit_password);
//        btnLogin = (TextView) findViewById(R.id.tx_login);
//        viewLine1 = findViewById(R.id.view_login_line_1);
//        viewLine2 = findViewById(R.id.view_login_line_2);
//
//        TextWatcher watcher = new TextWatcher() {
//            @Override
//            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//            }
//
//            @Override
//            public void onTextChanged(CharSequence s, int start, int before, int count) {
//            }
//
//            @Override
//            public void afterTextChanged(Editable s) {
//                if (!TextUtils.isEmpty(layEditPassword.getText()) && !TextUtils.isEmpty(layEditPhone.getText())) {
//                    viewLine1.setBackgroundColor(0xFF05131A);
//                    viewLine2.setBackgroundColor(0xFF05131A);
//
//                    btnLogin.setTextColor(0xFF05131A);
//                    btnLogin.setBackgroundResource(R.mipmap.login_enter_pressed);
//                } else {
//                    viewLine1.setBackgroundColor(0xFFB3B3B3);
//                    viewLine2.setBackgroundColor(0xFFB3B3B3);
//                    btnLogin.setTextColor(0xFFB3B3B3);
//                    btnLogin.setBackgroundResource(R.mipmap.login_enter_normal);
//                }
//
//            }
//        };
//
//        layEditPhone.addTextChangedListener(watcher);
//        layEditPassword.addTextChangedListener(watcher);
//
//        findViewById(R.id.tx_register).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this, RegisterAuthActivity.class);
//                intent.putExtra(RegisterCompleteActivity.EXTRA_ACTION, RegisterCompleteActivity.EXTRA_ACTION_REGISTER);
//                intent.putExtra(RegisterAuthActivity.EXTRA_AUTH_TITLE, getString(R.string.register_2));
//                startActivity(intent);
//            }
//        });
//
//        View layContainer = findViewById(R.id.lay_container);
//        layContainer.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                BaseUtil.hideSoftInputFromWindowAlways(LoginActivity.this);
//                return false;
//            }
//        });
//
//        layContainer.setPadding(0, (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT ? BaseUtil.getStatusBarHeight() : 0), 0, 0);
//
//        btnLogin.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                BaseUtil.hideSoftInputFromWindowAlways(LoginActivity.this);
//
//                if (checkLoginInfo(true)) {
//                    String phone = layEditPhone.getText().toString();
//                    String password = layEditPassword.getText().toString();
//                    requestLogin(phone, password);
//                }
//            }
//        });
//
//        findViewById(R.id.img_password_forget).setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(LoginActivity.this, RegisterAuthActivity.class);
//                intent.putExtra(RegisterCompleteActivity.EXTRA_ACTION, RegisterSetPwdActivity.EXTRA_ACTION_PWD_FORGET);
//                intent.putExtra(RegisterAuthActivity.EXTRA_AUTH_TITLE, getString(R.string.register_pwd_forget));
//                startActivity(intent);
//            }
//        });
//
//        loginInfoAutoInput();
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        BaseUtil.hideSoftInputFromWindowAlways(this);
//    }
//
//    private void requestLogin(final String phone, final String password) {
//        final LoadingDialog dlgLoading = new LoadingDialog.Builder(this).setCanCancel(false).create();
//        dlgLoading.show();
//
//        String platform = "paishen";
//        try {
//            ApplicationInfo appInfo = getPackageManager().getApplicationInfo(getPackageName(),
//                    PackageManager.GET_META_DATA);
//            platform = appInfo.metaData.getString("UMENG_CHANNEL");
//        } catch (PackageManager.NameNotFoundException ignored) {
//        }
//
//        PnwHttpManager.Token.getServer(TAG, BaseUtil.getAppVersionCode(), platform, new PnwOkCallbackAdapter<HttpRespData<ServiceInfo>>() {
//            @Override
//            public void onResponse(HttpRespData<com.paishen.peiniwan.frame.http.entity.ServiceInfo> response, boolean fresh) {
//                super.onResponse(response, fresh);
//                if (response.errcode == C.http.RESULT_SUCCESS) {
//                    PnwHttpManager.setCurrServiceUrl(response.data.server);
//
//                    PnwHttpManager.User.login(TAG, phone, password, new PnwOkCallbackAdapter<HttpRespUser<User>>() {
//                                @Override
//                                public void onResponse(HttpRespUser<User> response, boolean fresh) {
//                                    super.onResponse(response, fresh);
//                                    dlgLoading.dismiss();
//                                    if (response.errcode == C.http.RESULT_SUCCESS) {
//                                        SpfsUtil.write(C.sp.CURRENT_LOGIN_USER_ID, response.user.id);
//                                        final UserDao userDao = App.i().getDaoSession(response.user.id).getUserDao();
//                                        com.paishen.peiniwan.database.User dbUser = userDao.queryBuilder().where(UserDao.Properties.Id.eq(response.user.id)).unique();
//                                        dbUser = ModelConverter.mergeOrReplace(dbUser, response.user);
//                                        dbUser.setPhone(phone);
//                                        dbUser.setPassword(password);
//                                        dbUser.setApp_auto_login(true);
//                                        App.i().setUser(dbUser);
//                                        userDao.insertOrReplace(dbUser);
//
//                                        // 在App.i().setUser() 后，设置最后一次位置信息
//                                        App.i().locationAsync();
//
//                                        MobclickAgent.onProfileSignIn(phone);
//
//                                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
//                                        startActivity(intent);
//                                        ActivityCollector.finishAllExcept(MainActivity.class);
//                                    } else if (fresh && response.errcode != C.http.RESULT_LOGIN_EXPIRE &&
//                                            response.errcode != C.http.RESULT_LOGIN_OFFLINE) {
//                                        BaseUtil.showToast(response.errmsg);
//                                    }
//                                }
//
//                                @Override
//                                public void onFailure(Request request, Exception e) {
//                                    super.onFailure(request, e);
//                                    dlgLoading.dismiss();
//                                }
//                            }
//                    );
//                } else {
//                    BaseUtil.showToast(response.errmsg);
//                }
//            }
//
//            @Override
//            public void onFailure(Request request, Exception e) {
//                super.onFailure(request, e);
//                dlgLoading.dismiss();
//            }
//        });
//    }
//
//    @Override
//    public void onEventMainThread(BusEvent event) {
//        super.onEventMainThread(event);
//        if (event.type == BusEvent.TYPE_MAIN_THREAD) {
//            switch (event.action) {
//                case BusEvent.ACTION_LOGIN_AUTO_INPUT:
//                    loginInfoAutoInput();
//                    break;
//            }
//        }
//    }
//
//    private void loginInfoAutoInput() {
//        LogUtil.i("loginInfoAutoInput");
//        final com.paishen.peiniwan.database.User login = App.i().getUser();
//        if (login != null) {
//            if (!TextUtils.isEmpty(login.getPhone()) && !TextUtils.isEmpty(login.getPassword())) {
//                // third login don't auto input phone.
//                layEditPhone.setText(login.getPhone());
//                layEditPhone.setSelection(login.getPhone().length());
//                // layEditPassword.setText(login.getPassword());
//            }
//        }
//    }
//}
